 package com.aglhz.nature.view;


import java.util.ArrayList;
import java.util.List;
import java.util.Set;


import com.aglhz.nature.R;
import com.aglhz.nature.ListView.MyGridView;
import com.aglhz.nature.adapter.SearchAdapter;
import com.aglhz.nature.adapter.SearchRecommedAdapter;
import com.aglhz.nature.modle.item.SearchData;
import com.aglhz.nature.modle.item.ShopListData;
import com.aglhz.nature.presenter.SearchPresenter;
import com.aglhz.nature.utils.RecentSearchData;
import com.aglhz.nature.view.iv.SearchView;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;



import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;



import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

public class SeachActivity extends Activity implements SearchView {
	private SearchPresenter presenter;
	private SearchAdapter adapter;
	private SearchRecommedAdapter reAdapter;
	private String[] list;
	private ArrayList<String> alist;

	
	@Bind(R.id.back)
	ImageButton back;
	@Bind(R.id.search_et_input)
	EditText search_et_input;	
	@Bind(R.id.search_shop)
	TextView search_shop;
	@Bind(R.id.record_scroll)
	HorizontalScrollView record_scroll;
	@Bind(R.id.search_top_gridView)
	GridView search_top_gridView;
	@Bind(R.id.search_recommended_gridView)
	MyGridView search_recommended_gridView;
	@Bind(R.id.record_scroll_linlayout)
	LinearLayout record_scroll_linlayout;
	@Bind(R.id.clear_history)
	TextView clear_history;
	
	DbUtils db;
	
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.search_layout);
	 db = DbUtils.create(this);
	 ButterKnife.bind(this);
	 
	 
	 

	 
	 presenter = new SearchPresenter(this);
	// basePresenter register
			presenter.register();
			
			presenter.loadData();
	 
}

@OnClick(R.id.back)
public void back(){
	finish();
}

@OnClick(R.id.search_shop)
public void searchShop(){
	Intent intent=new Intent(SeachActivity.this,ShopListActivity.class);
	intent.putExtra("SeachActivity",search_et_input.getText().toString().trim());

	RecentSearchData data=new RecentSearchData();
	data.setContent(search_et_input.getText().toString());
	try {
		List<RecentSearchData> listDatas=db.findAll(RecentSearchData.class);
		if(null!=listDatas &&listDatas.size()>10){
			db.delete(listDatas.get(0));
		}else{
			
		}
		db.save(data);
	} catch (DbException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	startActivity(intent);
}


	@OnClick(R.id.clear_history)
	public void clearHistory(){
		 try {
			db.deleteAll(RecentSearchData.class);
			record_scroll_linlayout.removeAllViews();
		} catch (DbException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


@Override
public void showTopView(List<SearchData> data) {
	adapter=new SearchAdapter(data,SeachActivity.this);
	search_top_gridView.setAdapter(adapter);	
	
}

@Override
public void showRecommedView(List<ShopListData> data) {
	reAdapter=new SearchRecommedAdapter(data,SeachActivity.this);
	search_recommended_gridView.setAdapter(reAdapter);
	
}



@Override
protected void onResume() {
	try {
		List<RecentSearchData> listDatas=db.findAll(RecentSearchData.class);
		if (listDatas!=null&&listDatas.size()>0) {
			for (RecentSearchData data:listDatas) {
				TextView view =new TextView(this);
				view.setText(data.getContent());
				record_scroll_linlayout.addView(view);
			}
		}
		
		
		
	} catch (DbException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	super.onResume();
}
}
